package com.github.freegeese.maven.plugin.autocode.demo.base;

/**
 * 树状结构Service
 *
 * @param <M>
 * @param <E>
 * @param <ID>
 */
public class TreeableService<M extends TreeableMapper<E, ID>, E extends Treeable<ID>, ID> extends SortableService<M, E, ID> {

    @Override
    protected int move(E from, Position position) {
        M mapper = getMapper();
        E to = null;
        switch (position) {
            case TOP:
                to = mapper.selectFirst(from);
                break;
            case UP:
                to = mapper.selectPrevious(from);
                break;
            case DOWN:
                to = mapper.selectNext(from);
                break;
            case BOTTOM:
                to = mapper.selectLast(from);
                break;
        }
        // 没有找到目标行（两种可能：1.当前行在顶部，向上移动 2.当前行在底部，向下移动）
        if (null == to) {
            return 0;
        }
        // 同一行记录
        if (to.getId().equals(from.getId())) {
            return 0;
        }
        return mapper.move(from, to);
    }
}
